﻿using LPBToolsLib;
using LPBToolsLib.NetSocket;
using PLCConsole;
using StationNF;
using StationSer;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DExPLCDemo_PLC模拟器
{
    public partial class FDexPLCDemo : Form
    {
        int[] PLCSN = null;
        DExPacket tDExPacket=null;
        ushort TranID = 0;
        UDPClient tUDPClient=null;
        Recorder_RTxBytes tRTxBytes=new Recorder_RTxBytes();
        public FDexPLCDemo()
        {
            InitializeComponent();
            textBoxPLCSN.Text = getNewSN();
            int iLen = BaseTools.StrHexToInts(textBoxPLCSN.Text, out PLCSN);
            comboBoxProtrol.SelectedIndex = 0;
            comboBoxPLCType.SelectedIndex = 0;
        }

        string getNewSN()
        {
            return Guid.NewGuid().ToString("N").ToUpper();
        }

        private void buttonNewSN_Click(object sender, EventArgs e)
        {
            textBoxPLCSN.Text = getNewSN();
            int iLen = BaseTools.StrHexToInts(textBoxPLCSN.Text, out PLCSN);

        }

        void uploadDexDatas()
        {

            TranID++;
            tDExPacket= new DExPacket();
            tDExPacket.TranID = TranID;
            tDExPacket.ProtrolID = comboBoxProtrol.SelectedIndex + 1;
            tDExPacket.StatioinID = textBoxPLCSN.Text;

            DExOption_UploadRegValue_0x03 tOptUploadReg=new DExOption_UploadRegValue_0x03();
            tOptUploadReg.UploadReadRegStarAddr = 0x10;
            ushort[] tUploadDatas = new ushort[11];
            Random tRD =new Random();
            tUploadDatas[10] = (ushort)(3201 + tRD.Next(0, 200));
            for (int i = 0; i < 9; i++)
            {
                tUploadDatas[1 + i] = (ushort)(tRD.Next(0, 20000));
            }
            tOptUploadReg.UploadReadRegValues = tUploadDatas;
            tDExPacket.AppendOptDatas(tOptUploadReg.OptionDatas);

            DExOption_DeclarePLCType_0x01 tOptDecl = new DExOption_DeclarePLCType_0x01();
            tOptDecl.PLCRegStarAddr = 0;
            tOptDecl.PLCRegCount = 11;
            tOptDecl.PLCRType = comboBoxPLCType.SelectedIndex + 1;
            tDExPacket.AppendOptDatas(tOptDecl.OptionDatas);

            if (tUDPClient!=null)
            {
                tUDPClient.SendMsgTo(new IPEndPoint(IPAddress.Parse(textBoxServerIP.Text), (int)numericUpDownServerPort.Value),
                   tDExPacket.getPacketDatas());
            }
        }

        private void buttonStarStop_Click(object sender, EventArgs e)
        {
            Random tRD=new Random();
            if (tUDPClient == null)
            {
                tUDPClient = new UDPClient(new IPEndPoint(IPAddress.Any, tRD.Next(11000, 65000)));
                tUDPClient.OnRecDatas += TUDPClient_OnRecDatas;
                tUDPClient.RTxBytes = tRTxBytes;
                buttonStarStop.Text = "启动上传";
            }
            else
            {
                tUDPClient.CloseAll();
                tUDPClient = null;
                buttonStarStop.Text = "停止上传";
            }
        }

        private void TUDPClient_OnRecDatas(object sender, EndPoint SourPoint, byte[] RecData)
        {
            
        }

        int iSleep = 0;
        private void tDeamon_Tick(object sender, EventArgs e)
        {
            if (tUDPClient == null)
            {
                buttonStarStop.Text = "启动上传";
                toolStripStatusLabelUDPLocalInfo.Text = "";
            }
            else
            {
                buttonStarStop.Text = "停止上传";
                toolStripStatusLabelUDPLocalInfo.Text = "UDPListen@" + tUDPClient.ListenEndPoint.ToString();
                iSleep++;
                if (iSleep >10)
                {
                    iSleep = 0;
                    uploadDexDatas();
                }
            }
            while(tRTxBytes.RecCount>0)
            {
                richTextBoxRunLog.AppendText(tRTxBytes.getRecord().toLogStr()+"\r\n");
                RichTextGoBottom.GoBottom(richTextBoxRunLog);
            }
        }

        private void FDexPLCDemo_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (tUDPClient != null)
            {
                tUDPClient.CloseAll();
                tUDPClient = null;
            }
        }
    }
}
